package com.yueshop.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yueshop.auth.feign.MemberFeignService;
import com.yueshop.common.constant.AuthConstant;
import com.yueshop.common.utils.HttpUtils;
import com.yueshop.common.utils.R;
import com.yueshop.auth.vo.SocialUserVo;
import com.yueshop.common.vo.MemberRespVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理社交登录请求
 */
@Controller
public class Oauth2Controller {


    @Autowired
    MemberFeignService memberFeignService;

    /**
     * https://api.weibo.com/oauth2/access_token?
     * client_id=473350764&
     * client_secret=94b288e451d104d1048351bb67e6a18d&
     * grant_type=authorization_code&
     * redirect_uri=http://auth.yueshop.com/oauth2/weibo/success&
     * code=b0c4ad1de949037a609d90dc712617e9
     * @param code
     * @return
     */
    @GetMapping("/oauth2/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {

        Map<String,String> map = new HashMap<>();
        map.put("client_id","473350764");
        map.put("client_secret","94b288e451d104d1048351bb67e6a18d");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.yueshop.com/oauth2/weibo/success");
        map.put("code",code);


        HttpResponse post = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), null, map);
        //处理数据
        if(post.getStatusLine().getStatusCode()==200){
            //获取到了token 访问令牌
            //将返回的内容 转为SocialUserVo对象
            String json = EntityUtils.toString(post.getEntity());//将相应的内容转为json格式字符串
            SocialUserVo socialUserVo = JSON.parseObject(json, SocialUserVo.class);
            //调用远程 会员服务进行操作.
            //假如说说 会员是第一次进行登录,让 用户自动注册
            //调用远程服务进行 登录或者注册
            R r = memberFeignService.oauthLogin(socialUserVo);

            if(r.getCode()==0){//登录成功 要将用户信息 存如session中
                MemberRespVo data = r.getData(new TypeReference<MemberRespVo>() {});
                //向session中保存用户数据
                //TODO 在分布式系统中  session共享的问题
                //TODO springsession默认发令牌 是session=123123213 作用域默认是当前作用域(解决子域共享问题)
                //TODO redis存储的对象数据都是jdk序列化后的数据, 使用json序列化来序列化数据对象到redis中
                session.setAttribute(AuthConstant.LOGIN_USER,data);
                return "redirect:http://yueshop.com";

            }else{
                //说明登陆失败了
                return "redirect:http://auth.yueshop.com/login.html";
            }



        }else{
            //远程请求社交服务获取数据失败
            return "redirect:http://auth.yueshop.com/login.html";
        }



    }
}

